package cn.iocoder.yudao.module.contract.controller.admin.basicData;

import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicBrokerageDO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicPackingDO;
import cn.iocoder.yudao.module.contract.service.basicData.BasicPackingService;

@Tag(name = "管理后台 - 包装方式")
@RestController
@RequestMapping("/contract/basic-packing")
@Validated
public class BasicPackingController {

    @Resource
    private BasicPackingService basicPackingService;

    @PostMapping("/create")
    @Operation(summary = "创建包装方式")
    @PreAuthorize("@ss.hasPermission('contract:basic-packing:create')")
    public CommonResult<Long> createBasicPacking(@Valid @RequestBody BasicPackingSaveReqVO createReqVO) {
        return success(basicPackingService.createBasicPacking(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新包装方式")
    @PreAuthorize("@ss.hasPermission('contract:basic-packing:update')")
    public CommonResult<Boolean> updateBasicPacking(@Valid @RequestBody BasicPackingSaveReqVO updateReqVO) {
        basicPackingService.updateBasicPacking(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除包装方式")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('contract:basic-packing:delete')")
    public CommonResult<Boolean> deleteBasicPacking(@RequestParam("id") Long id) {
        basicPackingService.deleteBasicPacking(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得包装方式")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('contract:basic-packing:query')")
    public CommonResult<BasicPackingRespVO> getBasicPacking(@RequestParam("id") Long id) {
        BasicPackingDO basicPacking = basicPackingService.getBasicPacking(id);
        return success(BeanUtils.toBean(basicPacking, BasicPackingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得包装方式分页")
    @PreAuthorize("@ss.hasPermission('contract:basic-packing:query')")
    public CommonResult<PageResult<BasicPackingRespVO>> getBasicPackingPage(@Valid BasicPackingPageReqVO pageReqVO) {
        PageResult<BasicPackingDO> pageResult = basicPackingService.getBasicPackingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BasicPackingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出包装方式 Excel")
    @PreAuthorize("@ss.hasPermission('contract:basic-packing:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBasicPackingExcel(@Valid BasicPackingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BasicPackingDO> list = basicPackingService.getBasicPackingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "包装方式.xls", "数据", BasicPackingRespVO.class,
                        BeanUtils.toBean(list, BasicPackingRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得包装方式列表")
    public CommonResult<List<BasicPackingDO>> getBasicPackingList(@Valid BasicPackingDO basicPackingDO) {
        List<BasicPackingDO> listResult = basicPackingService.list(Wrappers.lambdaQuery(basicPackingDO));
        return success(listResult);
    }
}